﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using My3DGame.Game_States;

namespace My3DGame
{
    public class clsStateGameManager
    {
        #region Enum Game State Type

        public enum StateGame
        {
            INTRO,
            ABOUT,
            EXIT,
            HIGHTSCORE,
            MENU,
            OVER,
            START,
            WIN,
            HELP
        }

        #endregion
        
        #region Field
        private List<clsStateGame> currentStateGame;
        private clsIntroState introState;
        private clsAboutState aboutState;
        private clsExitState exitState;
        private clsHightScoreState hightScoreState;
        private clsMenuState menuState;
        private clsOverState overState;
        private clsStartState startState;
        private clsWinState winState;
        private clsHelpState helpState;
        #endregion

        public clsStateGameManager(Game1 game1)
        {
            // Khởi tạo 8 trạng thái game.
            introState = new clsIntroState(game1);
            aboutState = new clsAboutState(game1);
            exitState = new clsExitState(game1);
            hightScoreState = new clsHightScoreState(game1);
            menuState = new clsMenuState(game1);
            overState = new clsOverState(game1);
            startState = new clsStartState(game1);
            winState = new clsWinState(game1);
            helpState = new clsHelpState(game1);

            // Trạng thái đầu tiên của game là INTRO
            currentStateGame = new List<clsStateGame>();
            currentStateGame.Add(introState);
        }

        public void SetCurrentState(clsStateGame gameState)
        {
            currentStateGame.Clear();
            currentStateGame.Add(gameState);
        }

        // Lấy trạng thái hiện tại của game.
        public clsStateGame GetCurrentState()
        {
            try
            {
                return currentStateGame[0];
            }
            catch (Exception)
            {
                return null;
            }
        }

        public void NextState(Game1 game1)
        {
            switch (GetCurrentState().nextGameState)
            {
                case StateGame.ABOUT:
                    SetCurrentState(aboutState);
                    GetCurrentState().Initialize(game1);
                    GetCurrentState().LoadContent(game1);
                    break;
                case StateGame.EXIT:
                    SetCurrentState(exitState);
                    break;
                case StateGame.HIGHTSCORE:
                    SetCurrentState(hightScoreState);
                    break;
                case StateGame.INTRO:
                    SetCurrentState(introState);
                    break;
                case StateGame.MENU:
                    SetCurrentState(menuState);
                    GetCurrentState().Initialize(game1);
                    GetCurrentState().LoadContent(game1);
                    break;
                case StateGame.OVER:
                    SetCurrentState(overState);
                    break;
                case StateGame.START:
                    SetCurrentState(startState);
                    GetCurrentState().Initialize(game1);
                    GetCurrentState().LoadContent(game1);
                    break;
                case StateGame.WIN:
                    SetCurrentState(winState);
                    break;
                case StateGame.HELP:
                    SetCurrentState(helpState);
                    GetCurrentState().Initialize(game1);
                    GetCurrentState().LoadContent(game1);
                    break;
            }
        }

        public void Initialize(Game1 game1)
        {
            GetCurrentState().Initialize(game1);
        }

        public void LoadContent(Game1 game1)
        {
            GetCurrentState().LoadContent(game1);
        }

        public void Update(GameTime gameTime, Game1 game1)
        {
            GetCurrentState().Update(gameTime, game1);
        }

        public void Draw(GameTime gameTime, Game1 game1)
        {
            GetCurrentState().Draw(gameTime, game1);
        }
    }
}
